package com.zjtaixue.o2o.util;

import com.zjtaixue.o2o.exception.O2OException;

import java.io.*;

public class FileUtil {

    public static void inputStreamToFile(InputStream inputStream, File file){
        OutputStream outputStream=null;
        try{
            outputStream=new FileOutputStream(file);
            int byteRead=0;
            byte[] buffer=new byte[1024];
            while ((byteRead=inputStream.read(buffer))!=-1){
                outputStream.write(buffer,0,byteRead);
            }
        }catch (Exception e){
            throw new RuntimeException("调用inputStreamToFile异常:"+e.getMessage());
        }finally {
            try{
                if(outputStream!=null){
                    outputStream.close();
                }
                if(inputStream!=null){
                    inputStream.close();
                }
            }catch (IOException e){
                throw new RuntimeException("inputStreamToFile关闭io异常:"+e.getMessage());
            }
        }
    }
}
